home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr44 / modex32w.zip / MKALIGN.C < prev    next >
C/C++ Source or Header  |  1995-02-21  |  3KB  |  76 lines

  1. /* Generates all four possible mode X image/mask alignments, stores image 
  2. alignments in display memory, allocates memory for and generates mask 
  3. alignments, and fills out an AlignedMaskedImage structure. Image and mask must 
  4. both be in byte-per-pixel form, and must both be of width ImageWidth. Mask 
  5. maps isomorphically (one to one) onto image, with each 0-byte in mask masking
  6. off corresponding image pixel (causing it not to be drawn), and each non-0-byte
  7. allowing corresponding image pixel to be drawn. Returns 0 if failure, or # of 
  8. display memory addresses (4-pixel sets) used if success. For simplicity, 
  9. allocated memory is not deallocated in case of failure. Compiled with 
  10. Borland C++ 2.0 in C compilation mode. */
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include "modex32w.h"
  15.  
  16. unsigned int CreateAlignedMaskedImage(MaskedImage * ImageToSet,
  17.    unsigned int DispMemStart, char * Image, int ImageWidth,
  18.    int ImageHeight, char * Mask)
  19. {
  20.    int Align, ScanLine, BitNum, Size, TempImageWidth;
  21.    unsigned char MaskTemp;
  22.    unsigned int DispMemOffset = DispMemStart;
  23.    AlignedMaskedImage *WorkingAMImage;
  24.    char *NewMaskPtr, *OldMaskPtr;
  25.  
  26.    /* Generate each of the four alignments in turn */
  27.    for (Align = 0; Align < 4; Align++) {
  28.  
  29.       /* Allocate space for the AlignedMaskedImage struct for this
  30.          alignment */
  31.       if ((WorkingAMImage = ImageToSet->Alignments[Align] =
  32.             malloc(sizeof(AlignedMaskedImage))) == NULL)
  33.          return 0;
  34.  
  35.       WorkingAMImage->ImageWidth =
  36.             (ImageWidth + Align + 3) / 4; /* width in 4-pixel sets */
  37.  
  38.       WorkingAMImage->ImagePtr = DispMemOffset; /* image dest */
  39.  
  40.       /* Download this alignment of the image */
  41.       CopySystemToScreenX(0, 0, ImageWidth, ImageHeight, Align, 0,
  42.             Image, DispMemOffset, ImageWidth,
  43.             WorkingAMImage->ImageWidth * 4);
  44.  
  45.       /* Calculate the number of bytes needed to store the mask in
  46.          nibble (Map Mask-ready) form, then allocate that space */
  47.       Size = WorkingAMImage->ImageWidth * ImageHeight;
  48.  
  49.       if ((WorkingAMImage->MaskPtr = malloc(Size)) == NULL)
  50.          return 0;
  51.  
  52.       /* Generate this nibble oriented (Map Mask-ready) alignment of
  53.          the mask, one scan line at a time */
  54.       OldMaskPtr = Mask;
  55.       NewMaskPtr = WorkingAMImage->MaskPtr;
  56.       for (ScanLine = 0; ScanLine < ImageHeight; ScanLine++) {
  57.          BitNum = Align;
  58.          MaskTemp = 0;
  59.          TempImageWidth = ImageWidth;
  60.          do {
  61.             /* Set the mask bit for next pixel according to its alignment */
  62.             MaskTemp |= (*OldMaskPtr++ != 0) << BitNum;
  63.             if (++BitNum > 3) {
  64.                *NewMaskPtr++ = MaskTemp;
  65.                MaskTemp = BitNum = 0;
  66.             }
  67.          } while (--TempImageWidth);
  68.          /* Set any partial final mask on this scan line */
  69.          if (BitNum != 0) *NewMaskPtr++ = MaskTemp;
  70.       }
  71.       DispMemOffset += Size; /* mark off the space we just used */
  72.    }
  73.    return DispMemOffset - DispMemStart;
  74. }
  75.  
  76.